package com.monicash.monicash.main

import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.Window
import android.view.WindowManager
import androidx.core.view.ViewCompat
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.monicash.monicash.BafBase.BAFBaseActivity
import com.monicash.monicash.R
import com.monicash.monicash.main.fragment.HomeFragment
import com.monicash.monicash.main.fragment.PersonalFragment
import com.nadia.totoro.adapter.TabFragmentAdapter
import com.nadia.totoro.helper.BottomNavigationViewHelper

import kotlinx.android.synthetic.main.activity_main.*

/**
 * 首页
 */
class MainActivity : BAFBaseActivity<MainActivity>() {

	override fun initLayout(): Int = R.layout.activity_main

	override fun initParameter(bundle: Bundle?) {}

	override fun afterInjectView() {
		initView()
		//保留icon原图颜色
		navigation.itemIconTintList = null
		BottomNavigationViewHelper.disableShiftMode(navigation)
	}

	private fun initView() {
		val fragments = mutableListOf<Fragment>()
		//  val conversationListFragment = ConversationListFragment()
		fragments.add(0, HomeFragment().newInstance(0))
		// fragments.add(1, conversationListFragment)
		fragments.add(1, PersonalFragment().newInstance(1))
		viewPager.setEnableScroll(false) // false不可滑动
		viewPager.offscreenPageLimit = 2
		viewPager.adapter = TabFragmentAdapter(supportFragmentManager, fragments)
		viewPager.addOnPageChangeListener(onPageChangeListener)
		navigation.setOnNavigationItemSelectedListener(mOnListener)

		// 全屏显示
		val window : Window = getWindow()
		window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
		window.getDecorView().setSystemUiVisibility(
			View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
				or View.SYSTEM_UI_FLAG_LAYOUT_STABLE)
		window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
		window.setStatusBarColor(Color.TRANSPARENT)
		ViewCompat.getWindowInsetsController(getWindow().getDecorView())?.setAppearanceLightStatusBars(true)
	}

	/**
	 * 底部控制
	 */
	private val mOnListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
		when (item.itemId) {
			R.id.navigation_home -> {
				viewPager.currentItem = 0
				isActionBarBlackColor(true)
				return@OnNavigationItemSelectedListener true
			}
			R.id.navigation_person -> {
				viewPager.currentItem = 1
				isActionBarBlackColor(true)
				return@OnNavigationItemSelectedListener true
			}
		}
		false
	}

	private val onPageChangeListener = object : ViewPager.OnPageChangeListener {

		override fun onPageScrollStateChanged(position: Int) {}

		override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

		override fun onPageSelected(position: Int) {}
	}
}
